package com.ruoyi.violate.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.violate.domain.*;
import com.ruoyi.violate.service.IViolateCatalogueService;
import com.ruoyi.violate.service.IViolateClassNumberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.violate.service.IViolateTilteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 标题Controller
 * 
 * @author 谭拓宇
 * @date 2022-12-13
 */
@RestController
@RequestMapping("/violate/tilte")
public class ViolateTilteController extends BaseController
{
    @Autowired
    private IViolateTilteService violateTilteService;

    @Autowired
    private IViolateClassNumberService violateClassNumberService ;

    /**
     * 查询标题列表
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:list')")
    @GetMapping("/list")
    public TableDataInfo list(ViolateTilte violateTilte)
    {
        startPage();
        List<ViolateTilte> list = violateTilteService.selectViolateTilteList(violateTilte);
        System.out.println(list.size());
        return getDataTable(list);
    }

    /**
     * 查询标题列表
     */
//    @PreAuthorize("@ss.hasPermi('violate:tilte:list')")
    @GetMapping("/deleteSelectList")
    public int deleteSelectList(ViolateTilte violateTilte)
    {
        return  violateTilteService.deleteSelectList(violateTilte);
    }


    /**
     * 多级目录选择列表
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:typeList')")
    @GetMapping("/typeList/{ids}")
    public TableDataInfo typeList(@PathVariable String[] ids)
    {
        String[] types = new String[ids.length] ;
        for (int i = 0; i < ids.length; i++) {
            ViolateClassNumber violateClassNumber = violateClassNumberService.selectViolateClassNumberById(ids[i]);
            String type =   violateClassNumber.getTypeClass() ;
            types[i] = type ;
        }
        startPage();
        List<ViolateTilte> list = violateTilteService.selectViolateTypeList(types);
        System.out.println(list.size());
        TableDataInfo dataTable = getDataTable(list);
        System.out.println("多级目录选择列表执行！！！");
        return dataTable ;
    }

    /**
     * 一级目录选择列表
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:type1List')")
    @GetMapping("/type1List/{ids}")
    public TableDataInfo type1List(@PathVariable String[] ids)
    {
        String[] types = new String[ids.length] ;
        for (int i = 0; i < ids.length; i++) {
            ViolateClassNumber violateClassNumber = violateClassNumberService.selectViolateClassNumberById(ids[i]);
            String type =   violateClassNumber.getType1Class() ;
            types[i] = type ;
        }

        startPage();
        List<ViolateTilte> list = violateTilteService.selectViolateType1List(types);
        System.out.println(list.size());
        TableDataInfo dataTable = getDataTable(list);
        System.out.println("一级目录选择列表执行！！！");
        return dataTable ;

    }



    /**
     * 导出标题列表
     */
//    @PreAuthorize("@ss.hasPermi('violate:tilte:export')")
    @Log(title = "标题", businessType = BusinessType.EXPORT)
    @PostMapping("/exportZip")
    public void exportZip(HttpServletResponse response, ViolateTilte violateTilte) throws Exception {

        violateTilteService.exportZip(response , violateTilte);

    }

    /**
     * 导出标题列表
     */
//    @PreAuthorize("@ss.hasPermi('violate:tilte:export')")
    @Log(title = "标题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ViolateTilte violateTilte) throws Exception {

        violateTilteService.export(response , violateTilte);

    }

    /**
     * 获取标题详细信息
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(violateTilteService.selectViolateTilteById(id));
    }

    /**
     * 新增标题
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:add')")
    @Log(title = "标题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ViolateTilte violateTilte)
    {
        return toAjax(violateTilteService.insertViolateTilte(violateTilte));
    }

    /**
     * 修改标题
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:edit')")
    @Log(title = "标题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ViolateTilte violateTilte)
    {
        return toAjax(violateTilteService.updateViolateTilte(violateTilte));
    }

    /**
     * 删除标题
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:remove')")
    @Log(title = "标题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(violateTilteService.deleteViolateTilteByIds(ids));
    }

    /**
     * 删除全部数据
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:remove')")
    @Log(title = "标题", businessType = BusinessType.DELETE)
    @DeleteMapping("/all")
    public AjaxResult removeAll()
    {
        return toAjax(violateTilteService.deleteViolateTitleAll());
    }

    /**
     * 一键去重
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:distinct')")
    @GetMapping("/distinct")
    public AjaxResult distinct(){
        int i = violateTilteService.distinctViolateTilte();
        return  toAjax(i+1) ;
    }

    /**
     * 导入数据tilte
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:import')")
    @PostMapping("import")
    public AjaxResult importFile(@RequestBody ViolateTilte violateTilte) throws IOException {
        if(StringUtils.isEmpty(violateTilte.getFilePath()))
            return AjaxResult.error("请上传文件");
        return toAjax(violateTilteService.importFile(violateTilte.getFilePath()));
    }

    /**
     * 导入删除数据模板
     */
    @PreAuthorize("@ss.hasPermi('violate:tilte:importDelete')")
    @PostMapping("importDelete")
    public AjaxResult importFileDelete(@RequestBody ViolateTilte violateTilte) throws IOException {
        if(StringUtils.isEmpty(violateTilte.getFilePath()))
            return AjaxResult.error("请上传删除模板文件");
        return toAjax(violateTilteService.importFileDelete(violateTilte.getFilePath()));
    }
    /**
     * 导出删除url数据模板
     */
    @PostMapping({"/export/url"})
    public void exportUrl(HttpServletResponse response) {
        List<ViolateURL> list = new ArrayList();
        ViolateURL url = new ViolateURL();
        url.setUrl("www.xxx.xxx");
        list.add(url);
        ExcelUtil<ViolateURL> util = new ExcelUtil(ViolateURL.class);
        util.exportExcel(response, list, "URL删除模板");
    }

    /**
     * 导出删除标题数据模板
     */
    @PostMapping({"/export/title"})
    public void exportTetle(HttpServletResponse response) {
        List<Title> list = new ArrayList();
        Title title = new Title();
        title.setTitle("标题内容");
        list.add(title);
        ExcelUtil<Title> util = new ExcelUtil(Title.class);
        util.exportExcel(response, list, "标题删除模板");
    }
}
